package com.lottery.im.module.result.ui.good;

import android.annotation.SuppressLint;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.fondesa.recyclerviewdivider.RecyclerViewDivider;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.good.GameTypeSetterListBean;
import com.lottery.im.api.result.entity.good.GoodList;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.constant.RxBusCode;
import com.lottery.im.module.result.adapter.GoodListAdapter;
import com.lottery.im.module.result.adapter.SetterByGameTypeAdapter;
import com.lottery.im.module.result.presenter.GetListPresenter;
import com.lottery.im.module.result.view.GetListView;
import com.lottery.im.util.SpacesItemDecoration;
import com.lottery.im.util.rxbus.RxBus;
import com.lottery.im.util.rxbus.Subscribe;
import java.util.List;

/**
 * 作者：cyy
 * 时间：2018/10/8。
 */
@SuppressLint("ValidFragment") public class GetListFragment
    extends BaseSwipeFragment<GetListPresenter> implements GetListView {

  @BindView(R.id.tv_title) TextView tvTitle;
  @BindView(R.id.tv_empty) TextView mTvEmpty;
  @BindView(R.id.imageView) ImageView imageView;
  @BindView(R.id.rv_setter) RecyclerView mRvSetter;
  @BindView(R.id.rv_goodList) RecyclerView mRvGoodList;

  private String gameType;
  private SetterByGameTypeAdapter mSetterByGameTypeAdapter;
  private List<GameTypeSetterListBean> mGameTypeSetterListBeans;
  private GoodListAdapter mGoodListAdapter;
  private Animation mAnimationLeft;
  private Animation mAnimationRight;
  private LayoutAnimationController mLayoutAnimation;
  private int requestSetterId = 0;

  public GetListFragment(String gameType) {
    this.gameType = gameType;
  }

  @Override protected int getLayoutResId() {
    return R.layout.fragment_getlist;
  }

  @Override protected void initPresenter() {
    presenter = new GetListPresenter(mActivity, this, this);
    presenter.init();
  }

  @Override public void initView() {
    RxBus.get().register(this);
    mAnimationLeft = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_180_left);
    mAnimationRight = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_180_right);

    mLayoutAnimation = AnimationUtils
        .loadLayoutAnimation(mActivity, R.anim.layout_animation_from_top);
    //高手计划筛选界面
    mRvSetter.setLayoutManager(new GridLayoutManager(mActivity, 3));
    mRvSetter.addItemDecoration(new SpacesItemDecoration(10));
    mSetterByGameTypeAdapter = new SetterByGameTypeAdapter();
    mRvSetter.setAdapter(mSetterByGameTypeAdapter);

    //高手计划列表
    mRvGoodList.setLayoutManager(new LinearLayoutManager(mActivity));
    mGoodListAdapter = new GoodListAdapter();
    mRvGoodList.setAdapter(mGoodListAdapter);
    RecyclerViewDivider.with(mActivity)
        .size((int) mActivity.getResources().getDimension(R.dimen.line_size))
        .hideLastDivider()
        .build()
        .addTo(mRvGoodList);

    //高手刷筛选点击事件
    mSetterByGameTypeAdapter.setOnItemClickListener(
        (position, object) -> {
          for (int i = 0; i < mGameTypeSetterListBeans.size(); i++) {
            if (mGameTypeSetterListBeans.get(i).isSelect()) {
              mGameTypeSetterListBeans.get(i).setSelect(false);
              break;
            }
          }
          object.setSelect(true);
          mSetterByGameTypeAdapter.notifyList(mGameTypeSetterListBeans);
          requestSetterId = object.getSetterId();
          presenter.getListResult(object.getSetterId());
          tvTitle.setText(object.getTypeName());
          titleAnim();
        });
    mGoodListAdapter.setOnItemClickListener(
        (position, object) -> startFragment(GsPlanDetailFragment.create(object.getId(), gameType,
            mGameTypeSetterListBeans.get(0).getGameTypeName())));
    tvTitle.setText("高手计划");
    presenter.getSetterId(gameType);
  }

  @Override public void querySetterByGameTypeResult(List<GameTypeSetterListBean> list) {
    mSetterByGameTypeAdapter.notifyList(mGameTypeSetterListBeans = list);
    if (null != mGameTypeSetterListBeans && mGameTypeSetterListBeans.size() > 0) {
      requestSetterId = mGameTypeSetterListBeans.get(0).getSetterId();
      presenter.getListResult(mGameTypeSetterListBeans.get(0).getSetterId());
      tvTitle.setText(mGameTypeSetterListBeans.get(0).getTypeName());
    }else {
      mRvGoodList.setVisibility(View.GONE);
      mRvSetter.setVisibility(View.GONE);
      mTvEmpty.setVisibility(View.VISIBLE);
    }
  }

  @Override public void getListSuccess(List<GoodList> list) {
    for (int i = 0; i < list.size(); i++) {
      list.get(i).setPosition(i + 1);
    }
    mGoodListAdapter.notifyList(list);
  }

  @OnClick({ R.id.iv_back, R.id.ll_title }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_back:
        popBackStack();
        break;
      case R.id.ll_title:
        titleAnim();
        break;
    }
  }

  private void titleAnim() {
    if (mRvGoodList.getVisibility() == View.VISIBLE) {
      mRvGoodList.setVisibility(View.GONE);
      mRvSetter.setVisibility(View.VISIBLE);
      mRvSetter.setLayoutAnimation(mLayoutAnimation);
      mAnimationLeft.setFillAfter(true);
      imageView.startAnimation(mAnimationLeft);
    } else if (mRvSetter.getVisibility() == View.VISIBLE) {
      mRvGoodList.setVisibility(View.VISIBLE);
      mRvGoodList.setLayoutAnimation(mLayoutAnimation);
      mRvSetter.setVisibility(View.GONE);
      mAnimationRight.setFillAfter(true);
      imageView.startAnimation(mAnimationRight);
    }
  }

  @Subscribe(code = RxBusCode.ZAN_CODE)
  public void zan() {
    presenter.getListResult(requestSetterId);
  }

  @Override public void onDestroy() {
    super.onDestroy();
    RxBus.get().unRegister(this);
  }
}
